package org.jeecg.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.entity.DbItemCount;
import org.jeecg.modules.mapper.DbItemCountMapper;
import org.jeecg.modules.service.IDbItemCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: db_item_count
 * @Author: jeecg-boot
 * @Date:   2024-06-13
 * @Version: V1.0
 */
@Service
public class DbItemCountServiceImpl extends ServiceImpl<DbItemCountMapper, DbItemCount> implements IDbItemCountService {

    /**
     * 返回模块和计数项
     * @param id
     * @return
     */
    public List<DbItemCount> listItem(String id) {
        QueryWrapper<DbItemCount> queryWrapper = new QueryWrapper<>();
        //查找模块
        queryWrapper.eq("pur_id",id).isNull("parent_id");
        List<DbItemCount> json = this.list(queryWrapper);
        if(json==null||json.isEmpty()){
            return json;
        }
        List list = new ArrayList<>();
//        计数项
        json.forEach(item->{
            list.add(item.getId());
        });
        List<DbItemCount> json2 = this.list(new QueryWrapper<DbItemCount>().eq("pur_id", id).in("parent_id", list));
        Map<String, List<DbItemCount>> mdv = json2.stream().collect(Collectors.groupingBy(DbItemCount::getParentId));
        json.stream().forEach(item->item.setChildren(mdv.get(item.getId())));
        return json;
    }

    /**
     * 返回模块
     * @param id
     * @return
     */
    public List<DbItemCount> listModule(String id) {
        QueryWrapper<DbItemCount> queryWrapper = new QueryWrapper<>();
        //查找模块
        queryWrapper.eq("pur_id",id).isNull("parent_id");
        List<DbItemCount> json = this.list(queryWrapper);
        return json;
    }
}
